/**************玩家飞机类**************/
function PlayerPlane(src, x, y, speed) {
	this.node = document.createElement('img');
	this.src = src;
	this.x = x;
	this.y = y;
	this.speed = speed;
	this.act = [];
	this.bulletArr = [];
	this.canShoot = true;
	this.type = 'player';
	this.harm;
	this.shootFn;
	this.blood;

	this.init();
}

PlayerPlane.prototype.init = function() {
	// 获取初始值
	this.blood = planeArr[`${this.type}`].blood;
	this.harm = planeArr[`${this.type}`].harm;

	this.act.moveTop = false;
	this.act.moveBottom = false;
	this.act.moveLeft = false;
	this.act.moveRight = false;
	this.act.shoot = false;

	// 设置样式
	this.node.src = this.src;
	this.node.style.position = 'absolute';
	this.node.style.left = this.x + 'px';
	this.node.style.top = this.y + 'px';
	this.node.style.height = planeArr[`${this.type}`].height + 'px';
	this.node.style.width = planeArr[`${this.type}`].width + 'px';
	blood.setAttribute('data-percent',this.blood);
	blood.setAttribute('data-skill','/'+this.blood);

	// 设置子弹射击初始样式
	this.shootFn = this.shootDefault;

	canvas.appendChild(this.node);
};

PlayerPlane.prototype.toTop = function() {
	let that = this;
	if (this.y - this.speed > 0) {
		this.y -= this.speed;
		this.node.style.top = this.y + 'px';
	}
};
PlayerPlane.prototype.toBottom = function() {
	if (this.y + this.node.offsetHeight <= cHeight) {
		this.y += this.speed;
		this.node.style.top = this.y + 'px';
	}
};
PlayerPlane.prototype.toLeft = function() {
	if (this.x - this.speed > 0) {
		this.x -= this.speed;
		this.node.style.left = this.x + 'px';
	}
};
PlayerPlane.prototype.toRight = function() {
	if (this.x + this.node.offsetWidth <= cWidth) {
		this.x += this.speed;
		this.node.style.left = this.x + 'px';
	}
};

// 飞机行动
PlayerPlane.prototype.action = function() {
	if (this.act['moveTop']) {
		this.toTop();
	}
	if (this.act['moveBottom']) {
		this.toBottom();
	}
	if (this.act['moveLeft']) {
		this.toLeft();
	}
	if (this.act['moveRight']) {
		this.toRight();
	}
	if (this.act['shoot']) {
		this.shoot();
	}
};

// 飞机子弹移动和删除
PlayerPlane.prototype.controlBullet = function() {
	for (let i = 0; i < this.bulletArr.length; i++) {
		if (this.bulletArr[i].move()) {
			this.bulletArr.splice(i, 1);
			this.controlBullet();	// 补帧
			break;	// 当数组改变时跳出该帧动画
		}
	}
};

// 发射子弹
PlayerPlane.prototype.shoot = function() {
	if (this.canShoot) {
		this.shootFn();
		this.canShoot = false;
	}
};

// 发射初始子弹
PlayerPlane.prototype.shootDefault = function() {
	this.bulletArr.push(new Bullet('bullet1', this.x + 23, this.y - 14, 8));
	setTimeout(() => {
		this.canShoot = true;
	}, 150); // 控制子弹生成速度
};

// 发射子弹1
PlayerPlane.prototype.shootFn1 = function() {
	let bt1 = new Bullet(props['ufo1'].bullet, this.x + 13, this.y - 14, 8);
	let bt2 = new Bullet(props['ufo1'].bullet, this.x + 33, this.y - 14, 8);
	this.bulletArr.push(bt1,bt2);
	setTimeout(() => {
		this.canShoot = true;
	}, 150); // 控制子弹生成速度,为了保证不重叠不要太小
};

// 发射子弹2
PlayerPlane.prototype.shootFn2 = function() {
	let bt1 = new Bullet(props['ufo2'].bullet, this.x + 13, this.y - 14, 8);
	let bt2 = new Bullet(props['ufo2'].bullet, this.x + 33, this.y - 14, 8);
	this.bulletArr.push(bt1,bt2);
	setTimeout(() => {
		this.canShoot = true;
	}, 150);
};

// 获得道具
PlayerPlane.prototype.getProps = function(prop) {
	switch (prop) {
		case 'ufo1':
			this.shootFn = this.shootFn1;
			break;
		case 'ufo2':
			this.shootFn = this.shootFn2;
			break;
		case 'ufo3':
			break;
		default:
			break;
	}
};

PlayerPlane.prototype.injured = function() {
	if (this.blood - arguments[0] <= 0) {
		dieAnimation(this);
		return true;
	} else {
		this.blood -= arguments[0];
		return false;
	}
}
/**************玩家飞机类**************/
